<?php
/* -----------------------------------------------------------------
 *                        Aveghe My Blog
 * -----------------------------------------------------------------
 * Aveghe My Blog: made your blog easy!
 * Copyright (C) 2008 Elia Contini
 * 
 * Aveghe My Blog is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * Aveghe My Blog is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 */

class Comment
	{
		private $db;
		private $tablePrefix;
		
		public function __construct()
			{
				$this->db = Zend_Registry::get('db');
				$this->tablePrefix =  Zend_Registry::get('tablePrefix');
			}
				
		public function insertUpdateComment($commentID, $comment)
			{
				if(!isset($commentID) || $commentID == '')
					{
						// new comment (insert)
						$comment['comment_date'] = new Zend_Db_Expr('NOW()');
						
						/*while(list($key, $value) = each($comment))
							echo "$key : $value <br />"; die;*/
							
						$this->db->insert($this->tablePrefix.'comments', $comment);
					}
				else
					{
						// comment alredy exist (update)
						$where = 'comment_id = '.(int) $commentID;
						
						/*while(list($key, $value) = each($comment))
							echo "$key : $value <br />"; die;*/
						
						$this->db->update($this->tablePrefix.'comments', $comment, $where);
					}
			}
		
		public function getFileIdBy($commentID)
			{
				$sql = 'SELECT comment_file_id FROM '.$this->tablePrefix.'comments WHERE comment_id = "'.(int) $commentID.'";';
				$row = $this->db->fetchRow($sql);
				
				return (int) $row['comment_file_id'];
			}
			
		public function getCommentsForFile($fileID)
			{
				$sql = 'SELECT comment_author, comment_author_website, comment_date, comment_text FROM '.$this->tablePrefix.'comments WHERE comment_file_id ="'.(int) $fileID.'" AND comment_approved = "1" ORDER BY comment_date;';
				$row = $this->db->fetchAll($sql);
	
				return $row;
			}
			
		public function getCommentsQueue()
			{
				$sql = 'SELECT comment_id, comment_author, comment_author_website, comment_date, comment_text, comment_remote_ip FROM '.$this->tablePrefix.'comments WHERE comment_approved ="0";';
				$row = $this->db->fetchAll($sql);
	
				return $row;
			}
			
		public function delete($commentID)
			{
				$where = 'comment_id = '.(int) $commentID;
				$this->db->delete($this->tablePrefix.'comments', $where);
			}
	}
?>